package com.btpn.custody.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true, dynamicUpdate=true)
@Table(name = "M_GROUP_ROLE")
@NamedQueries({
	@NamedQuery(name  = "GroupRole.getByGroupName",
				query = "FROM GroupRole gr WHERE gr.groupName = :groupName")
})
public class GroupRole implements Serializable {

	/*=============================================== C O N S T A N T =========================================*/
	private static final long serialVersionUID = 4025794535499930671L;

	public static final String NQ__FIND__BY__NAME = "GroupRole.getByGroupName";
	
	/*=============================================== M E M B E R =============================================*/
	@Id 
	@GeneratedValue
	@Column(name = "ID")
	private Long id;
	
	@Column(name = "GROUP_NAME", columnDefinition="VARCHAR(50)")
	private String groupName;
	
	@Column(name = "GROUP_DESC", columnDefinition="VARCHAR(100)")
	private String groupDesc;
	
	@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, mappedBy = "groupRole")
	private List<User> users = new ArrayList<User>();
	
	@ElementCollection(targetClass = java.lang.String.class, fetch = FetchType.EAGER)
	@JoinTable(name = "T_AUTHORITIES", joinColumns = @JoinColumn(name = "GROUP_ROLE_ID")) @Column(name = "ROLE", nullable = false)
	private Set<String> roles = new HashSet<String>();
	
	@Column(name = "CREATED_DATE")
	private Date createdDate = new Date();

	@Column(name = "CREATED_BY")
	private String createdBy;

	@Column(name = "LAST_UPDATED_DATE")
	private Date lastUpdatedDate = new Date();

	@Column(name = "LAST_UPDATED_BY")
	private String lastUpdatedBy;
	
	@Override
	public String toString() {
		return groupName;
	}

	/*=============================================== G E T T E R   &   S E T T E R ===========================*/
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public String getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}

	public Date getLastUpdatedDate() {
		return lastUpdatedDate;
	}

	public void setLastUpdatedDate(Date lastUpdatedDate) {
		this.lastUpdatedDate = lastUpdatedDate;
	}

	public String getLastUpdatedBy() {
		return lastUpdatedBy;
	}

	public void setLastUpdatedBy(String lastUpdatedBy) {
		this.lastUpdatedBy = lastUpdatedBy;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public String getGroupDesc() {
		return groupDesc;
	}

	public void setGroupDesc(String groupDesc) {
		this.groupDesc = groupDesc;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}
	
	public Set<String> getRoles() {
		return roles;
	}
	
	public void setRoles(Set<String> roles) {
		this.roles = roles;
	}
}
